/***********************************************************************
 * 	Copyright @ 2011 Zhejiang Yutian Technology Co.,Ltd
 ==================================================================
 Project:        LaYouNet
 FileName:       TitleBar.java
 Describtion:    文件描述
 Author:         xufei
 Create Date:    2014-7-16 下午3:08:34
 *********************************************************************/
package net.hzlj.platform.enterprise.widght;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.annotation.StyleRes;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.*;
import android.widget.ImageView;
import net.hzlj.rl.zhangjiajie.R;

/**
 * @ClassName: TitleBar
 * @Description: 标题栏
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2015-12-2 11:23:18
 */
public class TitleBar extends FrameLayout {

    /**
     * 当前类型
     */
    private int mStyle;
    /**
     * 显示标题文字内容
     */
    private String mDisplayName;
    /**
     * 当前绑定AC
     */
    private Activity mBindActivity;

    private OnClickListener mAddOnClickListener;

    private OnClickListener mBackOnClickListener;

    public TitleBar(@NonNull Context context) {
        super(context);
    }

    public TitleBar(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleBar);
        this.mStyle = typedArray.getInt(R.styleable.TitleBar_display_style, Style.MAIN);
        this.mDisplayName = typedArray.getString(R.styleable.TitleBar_displayname);
        typedArray.recycle();

        buildTitleBar();
    }

    public TitleBar(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public TitleBar(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }



    private TextView mBackImageView;
    private TextView mTitleTextView,mBackTitleTextView,mFunctionImageView;
    private TextView mMainLeftView,mMainRightView;
    private RelativeLayout mMainLayout,mBgLayout,mBackMainLayout;
    private TextView mLineTextView;

    private void buildTitleBar() {
        addView(LinearLayout.inflate(getContext(), R.layout.widget_title_bar, null));
        if (isInEditMode()) {
            return;
        }
        mBackImageView = (TextView) findViewById(R.id.header_back_btn);
        mTitleTextView = (TextView) findViewById(R.id.header_title);
        mBackTitleTextView = (TextView) findViewById(R.id.header_back_title);
        mMainLayout = (RelativeLayout) findViewById(R.id.header_main_layout);
        mBackMainLayout = (RelativeLayout) findViewById(R.id.header_back_main_layout);
        mFunctionImageView = (TextView) findViewById(R.id.header_back_function_btn);
        mTitleTextView.setText(mDisplayName);
        mBackTitleTextView.setText(mDisplayName);

        mMainLeftView = (TextView) findViewById(R.id.header_main_left);
        mMainRightView = (TextView) findViewById(R.id.header_main_right);
        mMainLeftView.setVisibility(INVISIBLE);
        mMainRightView.setVisibility(INVISIBLE);

        mBgLayout = (RelativeLayout) findViewById(R.id.header_layout);
        mLineTextView = (TextView) findViewById(R.id.title_line_view);
        RefreshTitleBar(mStyle);

    }

    public void setOnMainLeftClickListener(OnClickListener listener,int icon,int visible){
        mMainLeftView.setVisibility(visible);
        if (visible == VISIBLE){
            mMainLeftView.setOnClickListener(listener);
            mMainLeftView.setBackgroundResource(icon);
        }
    }

    public TextView getBackView(){
        return mBackImageView;
    }

    public TextView getBackTitleView(){
        return mBackTitleTextView;
    }

    public TextView getBackFunctionView(){
        return mFunctionImageView;
    }

    public void setOnBackFunctionClickListener(OnClickListener listener,String title){
        mFunctionImageView.setText(title);
        mFunctionImageView.setOnClickListener(listener);
    }

    public void setOnBackLeftClickListener(OnClickListener listener,String title){
        mBackImageView.setOnClickListener(listener);
        mBackImageView.setText(title);
    }

    public void setOnMainLeftClickListener(OnClickListener listener,String title,int titleColor,int visible){
        mMainLeftView.setVisibility(visible);
        if (visible == VISIBLE){
            mMainLeftView.setOnClickListener(listener);
            mMainLeftView.setText(title);
            if (titleColor != 0){
                mMainRightView.setTextColor(titleColor);
            }
        }
    }

    public void setOnMainRightClickListener(OnClickListener listener,int icon,int visible){
        mMainRightView.setVisibility(visible);
        if (visible == VISIBLE){
            mMainRightView.setOnClickListener(listener);
            mMainRightView.setBackgroundResource(icon);
        }
    }

    public void setOnMainRightClickListener(OnClickListener listener,String title,int titleColor,int visible){
        mMainRightView.setVisibility(visible);
        if (visible == VISIBLE){
            mMainRightView.setOnClickListener(listener);
            mMainRightView.setText(title);
            if (titleColor != 0){
                mMainRightView.setTextColor(titleColor);
            }
        }
    }

    //设置标题栏的背景图片
    public void setBackBg(int colorBgResourceId,int visibleLine){
        mLineTextView.setVisibility(visibleLine);
        if (colorBgResourceId != 0){
            mBgLayout.setBackgroundResource(colorBgResourceId);
        }
    }

    public void updateFunctionState(int visible,String tip){
        mFunctionImageView.setVisibility(visible);
        mFunctionImageView.setText(tip);
    }

    public void RefreshTitleBar(int style) {
        mStyle = style;
        mBackMainLayout.setVisibility(GONE);
        mMainLayout.setVisibility(GONE);
        mFunctionImageView.setVisibility(GONE);
        if (style == Style.MAIN) {
            mMainLayout.setVisibility(VISIBLE);
        } else if (style == Style.BACK) {
            mBackMainLayout.setVisibility(VISIBLE);
            mFunctionImageView.setVisibility(GONE);
        } else if (style == Style.BACK_ADD) {
            mBackMainLayout.setVisibility(VISIBLE);
            mFunctionImageView.setVisibility(VISIBLE);
        }
        mBackImageView.setTag(mStyle);
        mBackImageView.setOnClickListener(mDefOnClickListener);
    }


    public void setDisplayName(String displayName) {
        this.mDisplayName = displayName;
        TextView title = (TextView) findViewById(R.id.header_title);
        title.setText(mDisplayName);
        TextView backTitle = (TextView) findViewById(R.id.header_back_title);
        backTitle.setText(mDisplayName);
    }

    public void bindActivity(Activity activity) {
        this.mBindActivity = activity;
    }

    private OnClickListener mDefOnClickListener = new OnClickListener() {
        public void onClick(View v) {
            if (v.getId() == R.id.header_back_btn) {
                mBindActivity.finish();
            }

        }
    };

    public static class Style {
        public static int MAIN = 0;
        public static int BACK = 1;
        public static int BACK_ADD = 2;
    }

}
